const maxSize = 30;
export default class timeQueue {
  constructor() {
    this.last = new Date();
    this.queue = [];
  }

  end(stamp = null) {
    if (!stamp) {
      stamp = new Date();
    }
    if (this.queue.length > maxSize) {
      this.queue.shift();
    }
    this.queue.push(stamp - this.last);
    this.last = stamp;

    return stamp;
  }

  start(stamp = null) {
    if (!stamp) {
      stamp = new Date();
    }
    this.last = stamp;
    return stamp;
  }

  duration() {
    return this.queue.length == 0
      ? -1
      : this.queue.reduce((a, b) => a + b) / this.queue.length;
  }

  fps() {
    const d = this.duration();
    return d == -1 ? -1 : 1000 / d;
  }
}
